﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoMapper;
using DTO;

namespace DAO
{
    public class HanhKhach_DAO
    {
        public static HanhKhach_DTO CreateHanhKhachDTO(HanhKhach hanhkhach)
        {
            HanhKhach_DTO hanhkhachDTO = new HanhKhach_DTO();
            Mapper.CreateMap<HanhKhach, HanhKhach_DTO>();
            hanhkhachDTO = Mapper.Map<HanhKhach, HanhKhach_DTO>(hanhkhach);
            return hanhkhachDTO;
        }

        public static HanhKhach CreateHanhKhachEFF(HanhKhach_DTO hanhkhachDTO)
        {
            HanhKhach hanhkhach = new HanhKhach();
            Mapper.CreateMap<HanhKhach_DTO, HanhKhach>();
            hanhkhach = Mapper.Map<HanhKhach_DTO, HanhKhach>(hanhkhachDTO);
            return hanhkhach;
        }

        public List<HanhKhach_DTO> GetHanhKhachList()
        {
            List<HanhKhach_DTO> listHanhKhach = new List<HanhKhach_DTO>();
            try
            {
                QLCBDataContext context = new QLCBDataContext();
                var query = from hk in context.HanhKhaches 
                                 select hk;
                foreach (var cb in query)
                {
                    HanhKhach_DTO hanhkhachDTO = CreateHanhKhachDTO(cb);
                    listHanhKhach.Add(hanhkhachDTO);
                }
            }
            catch (System.Exception ex)
            {
                string str = ex.Message;
            }
            return listHanhKhach;
        }

        public HanhKhach_DTO GetHanhKhachDetails(string maHanhKhach)
        {
            HanhKhach_DTO hanhkhachDTO = new HanhKhach_DTO();
            try
            {
                QLCBDataContext context = new QLCBDataContext();
                var query = from hk in context.HanhKhaches
                                 where hk.MaHanhKhach == maHanhKhach
                                 select hk;
                HanhKhach hanhkhachEF = query.First();
                hanhkhachDTO = CreateHanhKhachDTO(hanhkhachEF);
            }
            catch (System.Exception)
            {

            }
            return hanhkhachDTO;
        }

        public static string MaHanhKhachMax()
        {
            QLCBDataContext context = new QLCBDataContext();
            string query = (from hk in context.HanhKhaches select hk.MaHanhKhach).Max();
            return query;
        }

        public void InsertHanhKhach(HanhKhach_DTO hanhkhachDTO)
        {
            try
            {
                QLCBDataContext context = new QLCBDataContext();
                HanhKhach hanhkhach = new HanhKhach();
                hanhkhach.MaHanhKhach = hanhkhachDTO.MaHanhKhach;
                hanhkhach.TenHanhKhach = hanhkhachDTO.TenHanhKhach;
                hanhkhach.CMND = hanhkhachDTO.CMND;
                hanhkhach.DienThoai = hanhkhachDTO.DienThoai;
                context.HanhKhaches.InsertOnSubmit(hanhkhach);
                context.SubmitChanges();
            }
            catch
            {
            }
        }

        public void UpdateHanhKhach(HanhKhach_DTO hanhkhachDTO)
        {
            try
            {
                QLCBDataContext context = new QLCBDataContext();
                HanhKhach hanhkhach = new HanhKhach();
                hanhkhach = context.HanhKhaches.Single(hk => hk.MaHanhKhach == hanhkhachDTO.MaHanhKhach);
                hanhkhach.MaHanhKhach = hanhkhachDTO.MaHanhKhach;
                hanhkhach.TenHanhKhach = hanhkhachDTO.TenHanhKhach;
                hanhkhach.CMND = hanhkhachDTO.CMND;
                hanhkhach.DienThoai = hanhkhachDTO.DienThoai;
                context.SubmitChanges();
            }
            catch
            { }
        }

        public void DeleteHanhKhach(HanhKhach_DTO hanhkhachDTO)
        {
            try
            {
                QLCBDataContext context = new QLCBDataContext();
                HanhKhach hanhkhach = context.HanhKhaches.Single(hk => hk.MaHanhKhach == hanhkhachDTO.MaHanhKhach);
                context.HanhKhaches.DeleteOnSubmit(hanhkhach);
                context.SubmitChanges();
               
            }
            catch
            { }
        }
    }
}
